#include "stage_text.h"

STAGE_TEXT::STAGE_TEXT(int first_digit = 1, int second_digit = 1, CASH *cash=0):QObject(), QGraphicsItemGroup()
{

    this->cash=cash;

    this->setZValue(1);

    this->first_digit = first_digit;
    this->second_digit = second_digit;

    item_stage = new GRAPHICS_PIXMAP_ITEM(*cash->cash_pixmap->value("Res/text/stage_text/stage.png"), this);
    item_separator = new GRAPHICS_PIXMAP_ITEM(*cash->cash_pixmap->value("Res/text/stage_text/-.png"), this);

    item_first_digit = new GRAPHICS_PIXMAP_ITEM(*cash->cash_pixmap->value("Res/text/stage_text/" + QString::number(first_digit) + ".png"), this);
    item_second_digit = new GRAPHICS_PIXMAP_ITEM(*cash->cash_pixmap->value("Res/text/stage_text/" + QString::number(second_digit) + ".png"), this);

    tl_anims = new QTimeLine(1000, this);

    anim_init();
}

STAGE_TEXT::~STAGE_TEXT(){}

void STAGE_TEXT::anim_init()
{
    QGraphicsItemAnimation *anim_stage = new QGraphicsItemAnimation(this);
    anim_stage->setItem(item_stage);
    anim_stage->setTimeLine(tl_anims);

    anim_stage->setPosAt(1, QPointF(450, 317));
    anim_stage->setPosAt(0, anim_stage->posAt(1) - QPointF(1000, 0));
    item_stage->setPos(QPointF(-1000, 0));


    QGraphicsItemAnimation *anim_first_digit = new QGraphicsItemAnimation(this);
    anim_first_digit->setItem(item_first_digit);
    anim_first_digit->setTimeLine(tl_anims);

    anim_first_digit->setPosAt(1, QPointF(740, 317));
    anim_first_digit->setPosAt(0, anim_first_digit->posAt(1) + QPointF(1000, 0));
    item_first_digit->setPos(QPointF(G_W + 1000, 0));


    QGraphicsItemAnimation *anim_separator = new QGraphicsItemAnimation(this);
    anim_separator->setItem(item_separator);
    anim_separator->setTimeLine(tl_anims);

    anim_separator->setPosAt(1, QPointF(823, 317));
    anim_separator->setPosAt(0, anim_separator->posAt(1) + QPointF(3000, 0));
    item_separator->setPos(QPointF(G_W + 1000, 0));


    QGraphicsItemAnimation *anim_second_digit = new QGraphicsItemAnimation(this);
    anim_second_digit->setItem(item_second_digit);
    anim_second_digit->setTimeLine(tl_anims);

    anim_second_digit->setPosAt(1, QPointF(897, 317));
    anim_second_digit->setPosAt(0, anim_second_digit->posAt(1) + QPointF(5000, 0));
    item_second_digit->setPos(QPointF(G_W + 1000, 0));

    connect(tl_anims, SIGNAL(finished()), this, SLOT(anims_finished()));

    this->setVisible(false);
}

QPainterPath STAGE_TEXT::shape() const
{
    return QPainterPath();
}

void STAGE_TEXT::anim_show()
{
    this->setVisible(true);

    tl_anims->setDuration(1000);
    tl_anims->setDirection(QTimeLine::Forward);
    tl_anims->start();
}

void STAGE_TEXT::anim_hide()
{
    tl_anims->setDuration(500);
    tl_anims->setDirection(QTimeLine::Backward);
    tl_anims->start();
}

void STAGE_TEXT::anims_finished()
{
    if(tl_anims->direction() == QTimeLine::Backward){
        this->setVisible(false);

        if(second_digit==5) return; // fix от вываливания

        second_digit++;
        item_second_digit->setPixmap(*cash->cash_pixmap->value("Res/text/stage_text/" + QString::number(second_digit) + ".png"));
    }

}
